package com.zhangtai.modules.service.impl.salary;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhangtai.modules.dao.AwardApplicantDao;
import com.zhangtai.modules.entity.salary.AwardApplicantEntity;
import com.zhangtai.modules.service.salary.AwardApplicantService;
import com.zhangtai.utils.PageResult;
import com.zhangtai.utils.R;
import org.apache.commons.lang3.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
public class AwardApplicantServiceImpl extends ServiceImpl<AwardApplicantDao, AwardApplicantEntity> implements AwardApplicantService {

    @Override
    public PageResult queryPage( @RequestParam(value = "orgIds",required = false) List<Long> orgIds,
                                 @RequestParam(value = "applicantName", required = false) String applicantName,
                                @RequestParam(value = "usercode", required = false) String usercode,
                                @RequestParam(value = "startTime", required = false) String startTime,
                                @RequestParam(value = "endTime", required = false) String endTime,
                                @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {

        IPage<AwardApplicantEntity> page = this.page(
                new Page<AwardApplicantEntity>(pageNum,pageSize),
                new QueryWrapper<AwardApplicantEntity>()
                        .in(!CollectionUtils.isEmpty(orgIds),"unit_id",orgIds)
                        .eq(CollectionUtils.isEmpty(orgIds),"unit_id",null)
                        .like(!StringUtils.isEmpty(applicantName),"applicant_name",applicantName)
                        .like(!StringUtils.isEmpty(usercode),"usercode",usercode)
                        .ge(!StringUtils.isEmpty(startTime),"create_time",startTime)
                        .le(!StringUtils.isEmpty(endTime),"create_time",endTime)
                        .isNotNull("approval_status")
                        .eq("is_delete",0).orderByDesc("create_time")
        );

        return new PageResult(page);
    }

    @Override
    public AwardApplicantEntity getByIdE(Long id) {
            AwardApplicantEntity one = this.getOne(new QueryWrapper<AwardApplicantEntity>().eq("id", id).eq("is_delete", 0));

        return one;
    }

    @Override
    public R saveE(AwardApplicantEntity entity) {
        boolean b = this.save(entity);
        if (!b){
            return R.error();
        }
        return R.ok();

    }

    @Override
    public R updateByIdE(AwardApplicantEntity entity) {
        boolean b = this.updateById(entity);
        if (!b){
            return R.error();
        }
        return R.ok();
    }

    @Override
    public R removeOneById(Long id) {
            AwardApplicantEntity entity = new AwardApplicantEntity();
        entity.setId(id);
        entity.setIsDelete(1);
        boolean b = this.updateById(entity);
        if (!b){
            return R.error();
        }
        return R.ok();
    }

    @Override
    @Transactional
    public R removeByIdsE(List<Long> ids) {
        for (Long id : ids) {
            R r = removeOneById(id);
            if (!r.getFlag()){
                return r;
            }
        }
        return R.ok();
    }

}